#include "taskManager.h"

#define PUSH_BOX 150
#define GAME_TEST 300 
#define SETTING 450
#define LEVEL1 100
#define LEVEL2 230
#define LEVEL3 360
#define LEVEL4 490
#define LEVEL5 620

lv_obj_t *src = NULL; //屏幕对象
extern TaskMgr task_mgr;
extern lv_indev_t *indev_keypad;//引入键盘对象
lv_group_t *key_group;//键盘组

void taskmgrInit(TaskMgr * tm)
{
    memset(tm, 0, sizeof(TaskMgr));
    tm->isLoadingPageRunning = 1;
}

static void cg_event_callback(struct _lv_obj_t *obj, lv_event_t event)
{
    
    if (event == LV_EVENT_KEY) {
        const uint32_t *key = lv_event_get_data();
        UART_printf(&huart1,"\r\nevent = %d *key=%d",event,*key);
        UART_printf(&huart1,"\r\n pos_select = %d",lv_obj_get_x(choose_game.game_select));
        UART_printf(&huart1,"\r\n current_level = %d\n",my_box.currentLevel);
        if (*key == LV_KEY_UP) {
            ;
        } else if (*key == LV_KEY_DOWN) {
            ;
        } else if (*key == LV_KEY_LEFT) {
            if(lv_obj_get_x(choose_game.game_select) > 150){
                lv_obj_set_pos(choose_game.game_select,lv_obj_get_x(choose_game.game_select)-150,150);
            }
        } else if (*key == LV_KEY_RIGHT) {
            if(lv_obj_get_x(choose_game.game_select) < 450){
                lv_obj_set_pos(choose_game.game_select,lv_obj_get_x(choose_game.game_select)+150,150);
            }
        } else if (*key == LV_KEY_ENTER) {
            if(lv_obj_get_x(choose_game.game_select) == PUSH_BOX ){
                task_mgr.Enter_BC_request = 1;
            }
            if(lv_obj_get_x(choose_game.game_select) == GAME_TEST ){
                task_mgr.Enter_Extra_request = 1;
            }
            if(lv_obj_get_x(choose_game.game_select) == SETTING ){
                task_mgr.Enter_setting_request = 1;
            }
        } else if (*key == LV_KEY_HOME) {
            ;
        }
    }
}

static void bc_event_callback(struct _lv_obj_t *obj, lv_event_t event){ //选择推箱子关卡的回调函数
        if (event == LV_EVENT_KEY) {
        const uint32_t *key = lv_event_get_data();
        UART_printf(&huart1,"\r\nevent = %d *key=%d\n",event,*key);
        if (*key == LV_KEY_UP) {
            ;
        } else if (*key == LV_KEY_DOWN) {
            ;
        } else if (*key == LV_KEY_LEFT) {
            if(lv_obj_get_x(box_choose_level.level_select) > LEVEL1){
                lv_obj_set_pos(box_choose_level.level_select,lv_obj_get_x(box_choose_level.level_select)-130,100);
                my_box.currentLevel--;
            }
        } else if (*key == LV_KEY_RIGHT) {
            if(lv_obj_get_x(box_choose_level.level_select) < LEVEL5){
                lv_obj_set_pos(box_choose_level.level_select,lv_obj_get_x(box_choose_level.level_select)+130,100);
                my_box.currentLevel++;
            }
        } else if (*key == LV_KEY_ENTER) {
            UART_printf(&huart1,"currentLevel:%d\n",my_box.currentLevel);
            task_mgr.Enter_BoxGame_request = 1;
        } else if (*key == LV_KEY_HOME) { //回到上一界面，即回到选择游戏界面
            task_mgr.BackTo_CG_request = 1;
        }
    }
}


void del_LP(LP *page)
{
    lv_obj_del(page->imgMihoyo);
    lv_obj_del(page->main_container);
    lv_obj_del(page->obj_label);
    lv_obj_del(page->obj_bar);
    lv_style_reset(&page->img_style);
    lv_style_reset(&page->style_bg);
    lv_style_reset(&page->style_indic);
}
void create_CG(CG *page)
{
    //背景
    page->imgGirl = lv_img_create(src, NULL);
    lv_img_set_src(page->imgGirl, "F:/anime.bin");
    //对话框
    page->main_container = lv_obj_create(src, NULL);
    lv_obj_get_style_pad_top(page->main_container, 0);
    lv_obj_get_style_border_width(page->main_container, 0);    //边框宽
    lv_obj_set_style_local_radius(page->main_container, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->main_container, 700, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->main_container, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->main_container, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    //文本
    page->obj_label = lv_label_create(page->main_container, NULL);
    if (page->obj_label != NULL) {
        // lv_obj_get_style_text_color(obj_label,LV_COLOR_WHITE); // 设置文本颜色
        //lv_obj_get_style_text_font(page->obj_label, 20);                 // 设置文本字体大小
        lv_obj_set_style_local_text_font(page->obj_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->obj_label, "Ready for the game?\nPress Enter to start..."); // 设置文本内容
        lv_obj_align(page->obj_label, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 15);
    }
    //按键（切换至下一个界面）
    key_group = lv_group_create();//为键盘设计一个组
    lv_indev_set_group(indev_keypad, key_group);
    //创建一个按钮（屏幕上）
    page->btn = lv_btn_create(page->main_container, NULL);
    lv_obj_align(page->btn, NULL, LV_ALIGN_IN_RIGHT_MID, -20, 0);
    lv_obj_set_style_local_bg_opa(page->btn, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置不透明度
    
    //将图形按钮加入到组中
    lv_group_add_obj(key_group, page->btn);
    lv_obj_set_event_cb(page->btn, cg_event_callback);//按钮的事件
    //lv_group_set_editing(key_group, false);

    //为按钮添加文本
    page->btn_label = lv_label_create(page->btn,null);
    if (page->btn_label != NULL) {
        // lv_obj_get_style_text_color(obj_label,LV_COLOR_WHITE); // 设置文本颜色
        //lv_obj_get_style_text_font(page->obj_label, 20);                 // 设置文本字体大小
        lv_obj_set_style_local_text_font(page->btn_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->btn_label, "Enter ->"); // 设置文本内容
        lv_obj_align(page->btn_label, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 15);
    }
    
    //推箱子游戏容器
    page->game_box = lv_obj_create(src, NULL);
    lv_obj_get_style_pad_top(page->game_box, 0);
    lv_obj_get_style_border_width(page->game_box, 0);    //边框宽
    lv_obj_set_style_local_radius(page->game_box, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->game_box, 120, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->game_box, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->game_box, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    lv_obj_set_pos(page->game_box,150,150);

    //容器的文本
    page->game_box_label = lv_label_create(page->game_box,null);
    if (page->game_box_label != NULL) {
        lv_obj_set_style_local_text_font(page->game_box_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->game_box_label, "Pushing Box!"); // 设置文本内容
        lv_obj_align(page->game_box_label, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 50);
    }

    //选择游戏的框框
    page->game_select = lv_btn_create(src, NULL);
    lv_obj_align(page->game_select, NULL, LV_ALIGN_IN_RIGHT_MID, -20, 0);
    lv_obj_set_style_local_bg_opa(page->game_select, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_10); // 设置不透明度
    lv_obj_set_size(page->game_select, 120, 120);
    lv_obj_set_pos(page->game_select,150,150); 

    //测试选择Extra页面
    page->game_test = lv_obj_create(src, NULL);
    lv_obj_get_style_pad_top(page->game_test, 0);
    lv_obj_get_style_border_width(page->game_test, 0);    //边框宽
    lv_obj_set_style_local_radius(page->game_test, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->game_test, 120, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->game_test, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->game_test, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    lv_obj_set_pos(page->game_test,300,150);

    //Extra的文本
    page->Extra_label = lv_label_create(page->game_test,null);
    if (page->game_test != NULL) {
        lv_obj_set_style_local_text_font(page->Extra_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->Extra_label, "Info"); // 设置文本内容
        lv_obj_align(page->Extra_label, NULL, LV_ALIGN_IN_TOP_MID, 15, 50);
    }

    //设置按钮
    page->settings = lv_obj_create(src, NULL);
    lv_obj_get_style_pad_top(page->settings, 0);
    lv_obj_get_style_border_width(page->settings, 0);    //边框宽
    lv_obj_set_style_local_radius(page->settings, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->settings, 120, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->settings, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->settings, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    lv_obj_set_pos(page->settings,450,150);

    //设置的文本
    page->settings_label = lv_label_create(page->settings,null);
    if (page->settings != NULL) {
        lv_obj_set_style_local_text_font(page->settings_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->settings_label, "User settings!"); // 设置文本内容
        lv_obj_align(page->settings_label, NULL, LV_ALIGN_IN_TOP_LEFT, 15, 50);
    }
    printf_Mem();
}
void del_CG(CG *page){
    lv_obj_del(page->imgGirl);
    lv_obj_del(page->main_container);
    lv_obj_del(page->game_box);
    lv_obj_del(page->game_select);
    lv_obj_del(page->game_test);
    lv_obj_del(page->settings);
}
void create_box_level(box_CL* page){
    //背景
    page->img_BG = lv_img_create(src, NULL);
    switch (SysTick->VAL % 5)
    {
    case 0:
        lv_img_set_src(page->img_BG, "F:/datongling.bin");
        break;
    case 1:
        lv_img_set_src(page->img_BG, "F:/nilu.bin");
        break;
    case 2:
        lv_img_set_src(page->img_BG, "F:/sex_girl.bin");
        break;
    case 3:
        lv_img_set_src(page->img_BG, "F:/anime.bin");
        break;
    case 4:
        lv_img_set_src(page->img_BG, "F:/extra.bin");
        break;
    default:
        break;
    }

    //对话框
    page->main_container = lv_obj_create(src, NULL);
    lv_obj_get_style_pad_top(page->main_container, 0);
    lv_obj_get_style_border_width(page->main_container, 0);    //边框宽
    lv_obj_set_style_local_radius(page->main_container, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->main_container, 700, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->main_container, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->main_container, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);

    page->obj_label = lv_label_create(page->main_container, NULL);
    if (page->obj_label != NULL) {
        // lv_obj_get_style_text_color(obj_label,LV_COLOR_WHITE); // 设置文本颜色
        //lv_obj_get_style_text_font(page->obj_label, 20);                 // 设置文本字体大小
        lv_obj_set_style_local_text_font(page->obj_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->obj_label, "Choose a level!\nPress Enter to start..."); // 设置文本内容
        lv_obj_align(page->obj_label, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 15);
    }
    //按键（切换至下一个界面）
    key_group = lv_group_create();//为键盘设计一个组
    lv_indev_set_group(indev_keypad, key_group);
    //创建一个按钮（屏幕上）
    page->btn = lv_btn_create(page->main_container, NULL);
    lv_obj_align(page->btn, NULL, LV_ALIGN_IN_RIGHT_MID, -20, 0);
    lv_obj_set_style_local_bg_opa(page->btn, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置不透明度
    
    //将图形按钮加入到组中
    lv_group_add_obj(key_group, page->btn);
    lv_obj_set_event_cb(page->btn, bc_event_callback);//按钮的事件
    //lv_group_set_editing(key_group, false);

    //为按钮添加文本
    page->btn_label = lv_label_create(page->btn,null);
    if (page->btn_label != NULL) {
        // lv_obj_get_style_text_color(obj_label,LV_COLOR_WHITE); // 设置文本颜色
        //lv_obj_get_style_text_font(page->obj_label, 20);                 // 设置文本字体大小
        lv_obj_set_style_local_text_font(page->btn_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->btn_label, "Enter ->"); // 设置文本内容
        lv_obj_align(page->btn_label, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 15);
    }
    //选择关卡的框框
    page->level_select = lv_btn_create(src, NULL);
     lv_obj_align(page->level_select, NULL, LV_ALIGN_IN_RIGHT_MID, -20, 0);
    lv_obj_set_style_local_bg_opa(page->level_select, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_10); // 设置不透明度
    lv_obj_set_size(page->level_select, 120, 120);
    lv_obj_set_pos(page->level_select,100,100); 
    //level1 控件
    page->level1 = lv_obj_create(src, NULL);
    lv_obj_get_style_pad_top(page->level1, 0);
    lv_obj_get_style_border_width(page->level1, 0);    //边框宽
    lv_obj_set_style_local_radius(page->level1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->level1, 120, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->level1, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->level1, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    lv_obj_set_pos(page->level1,100,100);
    //level1 文本
    page->level1_label = lv_label_create(page->level1,null);
    if (page->level1_label != NULL) {
        lv_obj_set_style_local_text_font(page->level1_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->level1_label, "Level 1"); // 设置文本内容
        lv_obj_align(page->level1_label, NULL, LV_ALIGN_IN_TOP_LEFT, 30, 50);
    }

     //level2 控件
    page->level2 = lv_obj_create(src, NULL);
    lv_obj_set_style_local_radius(page->level2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->level2, 120, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->level2, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->level2, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    lv_obj_set_pos(page->level2,230,100);

    //level2 文本
    page->level2_label = lv_label_create(page->level2,null);
    if (page->level2_label != NULL) {
        lv_obj_set_style_local_text_font(page->level2_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->level2_label, "Level 2"); // 设置文本内容
        lv_obj_align(page->level2_label, NULL, LV_ALIGN_IN_TOP_LEFT, 30, 50);
    }  

    //level3 控件
    page->level3 = lv_obj_create(src, NULL);
    lv_obj_set_style_local_radius(page->level3, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->level3, 120, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->level3, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->level3, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    lv_obj_set_pos(page->level3,360,100);

    //level3 文本
    page->level3_label = lv_label_create(page->level3,null);
    if (page->level3_label != NULL) {
        lv_obj_set_style_local_text_font(page->level3_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->level3_label, "Level 3"); // 设置文本内容
        lv_obj_align(page->level3_label, NULL, LV_ALIGN_IN_TOP_LEFT, 30, 50);
    } 

    //level4 控件
    page->level4 = lv_obj_create(src, NULL);
    lv_obj_set_style_local_radius(page->level4, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->level4, 120, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->level4, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->level4, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    lv_obj_set_pos(page->level4,490,100);

    //level4 文本
    page->level4_label = lv_label_create(page->level4,null);
    if (page->level4_label != NULL) {
        lv_obj_set_style_local_text_font(page->level4_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->level4_label, "Level 4"); // 设置文本内容
        lv_obj_align(page->level4_label, NULL, LV_ALIGN_IN_TOP_LEFT, 30, 50);
    }

    //level5 控件
    page->level5 = lv_obj_create(src, NULL);
    lv_obj_set_style_local_radius(page->level5, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,10);    //圆角大小        
    lv_obj_set_size(page->level5, 120, 120);           // 设置尺寸
    lv_obj_set_style_local_bg_opa(page->level5, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_60); // 设置背景不透明度
    lv_obj_align(page->level5, src, LV_ALIGN_IN_BOTTOM_MID , 0, -20);
    lv_obj_set_pos(page->level5,620,100);

    //level5 文本
    page->level5_label = lv_label_create(page->level5,null);
    if (page->level5_label != NULL) {
        lv_obj_set_style_local_text_font(page->level5_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT ,&mengTi);
        lv_label_set_text(page->level5_label, "Level 5"); // 设置文本内容
        lv_obj_align(page->level5_label, NULL, LV_ALIGN_IN_TOP_LEFT, 30, 50);
    }

    //根据之前的状态画选框
    switch (my_box.currentLevel)
    {
    case 1:
        lv_obj_set_pos(box_choose_level.level_select,100,100);
        break;
    case 2:
        lv_obj_set_pos(box_choose_level.level_select,230,100);
        break;
    case 3:
        lv_obj_set_pos(box_choose_level.level_select,360,100);
        break;
    case 4:
        lv_obj_set_pos(box_choose_level.level_select,490,100);
        break;
    case 5:
        lv_obj_set_pos(box_choose_level.level_select,620,100);
        break;
    default:
        break;
    }
    printf_Mem();

}
void del_box_level(box_CL* page){
    lv_obj_del(page->img_BG);
    lv_obj_del(page->level1);
    lv_obj_del(page->level2);
    lv_obj_del(page->level3);
    lv_obj_del(page->level4);
    lv_obj_del(page->level5);
    lv_obj_del(page->main_container);
    lv_obj_del(page->level_select);
}
void create_box_game(){
    mygame_loadmap(&my_box);
}
void printf_Mem(){
    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    UART_printf(&huart1,"used: %6d (%3d %%), frag: %3d %%, biggest free: %6d\n", (int)mon.total_size - mon.free_size,
                mon.used_pct,
                mon.frag_pct,
                (int)mon.free_biggest_size);
}